/**
 * 
 */
$(function() {
	$("#rq").change(function() {
		var $date = $(this).val();
		if ($date == null || $date == "")
			return;
		var $year = $date.substr(0, 4);
		var $month = $date.substr(5, 2);
		getSale($year, $month);
	});

	getOrder();
	
	$("#prePage").click(function(){
		if($page <= 1){
			alert("已经是第一页了！");
		}else{
			$page--;
			getOrder();
		}
	});
	
	
	$("#pageGo").click(function(){
		$(this).val("");
	});
	
	$("#pageGo").blur(function(){
		if($(this).val() == "")	
			setPage();
	});
	
	$("#pageGo").keyup(function(event){
		if(event.keyCode == 13)	{
			var $num = $(this).val();
			if(!isNaN($num)){
				if($num < 1 || $num > $count){
					alert("超出页数！");
				}else{
					$page = $num;
					getOrder();
				}
			}else{
				$(this).val("");
			}
		}
	});
	
	
	$("#nextPage").click(function(){
		if($page >= $count){
			alert("已经是最后页了！");
		}else{
			$page++;
			getOrder();
		}
		
	});
});

function getSale($year, $month) {
	$.ajax({
		url : "charts.do?method=sale",
		type : "post",
		data : {
			year : $year,
			month : $month
		},
		error : function(request) {
			alert("出现问题，最好重启电脑！");
			$("#rq").val("");
		},
		success : function(data) {
			if (data.status == 200) {
				$("#monthSale").text(data.monthSale);
				$("#cashSale").text(data.cashSale);
				$("#sales").text(data.sales);
			} else {
				alert(data.msg);
			}
		}
	});
}

var $page = 1;
var $count = 0;

function setPage(){
	$("#pageGo").attr("placeholder", "共" + $count + "页")
	.val("第" + $page + "页");
}

function getOrder() {
	$
			.ajax({
				url : "charts.do?method=order",
				type : "post",
				data : {
					page : $page
				},
				error : function(request) {
					alert("出现问题，最好重启电脑！");
				},
				success : function(data) {
					if (data.status == 200) {
						$(".chartsTr").remove();
						$count = data.count;
						setPage();
						$
								.each(
										data.content,
										function() {
											var $element = '<tr class="chartsTr"><td style="width:18%;">'
													+ this.ss
													+ '</td><td style="width:18%;">'
													+ this.yj
													+ '</td><td style="width:18%;">'
													+ this.rq
													+ '</td><td style="width:45%;">';
											$.each(this.list, function() {
												$element += '￥' + this.zj
														+ '： ' + this.sl + '*'
														+ this.name + '<br/>';
											});
											$element += '</td></tr>';
											$("#tBody").append($element);
										});
					} else {
						alert(data.msg);
					}
				}
			});
}